package com.penghe.www.baselibrary.ui.activities

import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.annotation.LayoutRes
import android.support.annotation.StringRes
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import com.penghe.www.baselibrary.R


/**
 * Created by alm on 17-6-1.
 * AppCompatActivity的父类
 */
abstract class BaseAppcompatActivity : AppCompatActivity() {

    private var toolbar: Toolbar? = null
    private var onClickListener: View.OnClickListener? = null
    private var content: FrameLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onClickListener = View.OnClickListener{ _ -> onBackPressed() }
        doBeforeSetLayout()

        setContentView(R.layout.activity_base_appcompat)

        initThisView()
        initThisData()
        initThisEvents()

        doBeforeInitOthers()
        initViews()
        initViewData()
        initOtherData()
        initEvents()
    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        return createOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return optionsItemSelected(item)
    }

    /**
     * 在设置布局之前需要进行的操作
     */
    protected abstract fun doBeforeSetLayout()

    /**
     * 设置布局

     * @return 布局id
     */
    @LayoutRes
    protected abstract fun setLayout(): Int

    /**
     * 在设置布局之后，进行其他操作之前，所需要初始化的数据
     */
    protected abstract fun doBeforeInitOthers()

    /**
     * 初始化布局控件
     */
    protected abstract fun initViews()

    /**
     * 初始化控件数据
     */
    protected abstract fun initViewData()

    /**
     * 初始化其他数据
     */
    protected abstract fun initOtherData()

    /**
     * 初始化事件
     */
    protected abstract fun initEvents()

    /**
     * 设置菜单

     * @param menu 菜单
     * *
     * @return 只是重写 public boolean onCreateOptionsMenu(Menu menu)
     */
    protected abstract fun createOptionsMenu(menu: Menu): Boolean

    /**
     * 设置菜单监听

     * @param item 菜单的item
     * *
     * @return true表示处理了监听事件
     */
    protected abstract fun optionsItemSelected(item: MenuItem): Boolean

    override fun onDestroy() {
        super.onDestroy()
        onClickListener = null
    }

    private fun initThisView() {
        toolbar = findViewById(R.id.toolbar) as Toolbar
        content = findViewById(R.id.content) as FrameLayout
    }

    private fun initThisData() {
        toolbar!!.setNavigationIcon(R.drawable.back)
        setSupportActionBar(toolbar)
        val view = layoutInflater.inflate(setLayout(), null)
        content!!.addView(view)
    }

    private fun initThisEvents() {
        toolbar!!.setNavigationOnClickListener(onClickListener)
    }

    //下面都是自定义的函数了

    /**
     * 设置title返回按钮的处理事件

     * @param onClickListener 返回按钮的处理事件
     */
    protected fun setTitleBackOnClickListener(onClickListener: View.OnClickListener) {
        toolbar!!.setNavigationOnClickListener(onClickListener)
    }

    /**
     * 隐藏标题栏的返回按钮
     */
    protected fun hideTitleBackButton() {
        toolbar!!.setNavigationIcon(null)
    }

    /**
     * 显示标题栏的返回按钮
     */
    protected fun showTitleBackButton() {
        toolbar!!.setNavigationIcon(R.drawable.back)
    }

    /**
     * 隐藏标题栏
     */
    protected fun hideTitle() {
        val supportActionBar = supportActionBar ?: throw RuntimeException("supportActionBar is null")
        supportActionBar.hide()
    }

    /**
     * 显示标题栏
     */
    protected fun showTitle() {
        val supportActionBar = supportActionBar ?: throw RuntimeException("supportActionBar is null")
        supportActionBar.show()
    }

    /**
     * 设置标题栏的内容

     * @param titleResId 标题栏的资源id
     */
    protected fun setTitleText(@StringRes titleResId: Int) {
        val supportActionBar = supportActionBar ?: throw RuntimeException("方法->setTitleText() 必须在 方法->setLayout() 之后执行")
        supportActionBar.setTitle(titleResId)
    }

    /**
     * 设置标题栏的内容

     * @param titleText 标题栏内容
     */
    protected fun setTitleText(titleText: String) {
        val supportActionBar = supportActionBar ?: throw RuntimeException("方法->setTitleText() 必须在 方法->setLayout() 之后执行")
        supportActionBar.setTitle(titleText)
    }

    /**
     * 设置标题栏左边的图片
     * @param resId 标题栏左边的图片资源id
     */
    protected fun setTitleBackIcon(@DrawableRes resId: Int) {
        toolbar!!.setNavigationIcon(resId)
    }
}
